{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import fetch_20newsgroups  # 从sklearn.datasets里导入新闻数据抓取器 fetch_20newsgroups\n",
    "from sklearn.model_selection import  train_test_split\n",
    "from sklearn.feature_extraction.text import CountVectorizer  # 从sklearn.feature_extraction.text里导入文本特征向量化模块\n",
    "from sklearn.metrics import classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.naive_bayes import MultinomialNB,GaussianNB,BernoulliNB    # 从sklean.naive_bayes里导入朴素贝叶斯模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import RidgeClassifier,LogisticRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neural_network import MLPClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import lightgbm as lgb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from lightgbm import LGBMClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier,AdaBoostClassifier,BaggingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import xgboost\n",
    "from xgboost import XGBClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.svm import LinearSVC,SVC"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 数据获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18846\n"
     ]
    }
   ],
   "source": [
    "#1.数据获取\n",
    "news = fetch_20newsgroups(subset='all')\n",
    "print(len(news.data))  # 输出数据的条数：18846"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#2.数据预处理：训练集和测试集分割，文本特征向量化\n",
    "#分割\n",
    "X_train,X_test,y_train,y_test = train_test_split(news.data,news.target,test_size=0.25,random_state=33) # 随机采样25%的数据样本作为测试集\n",
    "# print(X_train[0])  #查看训练样本\n",
    "# print(y_train[0:100])  #查看标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#文本向量化\n",
    "#文本特征向量化\n",
    "vec = CountVectorizer()\n",
    "X_train = vec.fit_transform(X_train)\n",
    "X_test = vec.transform(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.使用朴素贝叶斯进行训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of Naive Bayes Classifier is: 0.8397707979626485\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.86      0.86      0.86       201\n",
      "           comp.graphics       0.59      0.86      0.70       250\n",
      " comp.os.ms-windows.misc       0.89      0.10      0.17       248\n",
      "comp.sys.ibm.pc.hardware       0.60      0.88      0.72       240\n",
      "   comp.sys.mac.hardware       0.93      0.78      0.85       242\n",
      "          comp.windows.x       0.82      0.84      0.83       263\n",
      "            misc.forsale       0.91      0.70      0.79       257\n",
      "               rec.autos       0.89      0.89      0.89       238\n",
      "         rec.motorcycles       0.98      0.92      0.95       276\n",
      "      rec.sport.baseball       0.98      0.91      0.95       251\n",
      "        rec.sport.hockey       0.93      0.99      0.96       233\n",
      "               sci.crypt       0.86      0.98      0.91       238\n",
      "         sci.electronics       0.85      0.88      0.86       249\n",
      "                 sci.med       0.92      0.94      0.93       245\n",
      "               sci.space       0.89      0.96      0.92       221\n",
      "  soc.religion.christian       0.78      0.96      0.86       232\n",
      "      talk.politics.guns       0.88      0.96      0.92       251\n",
      "   talk.politics.mideast       0.90      0.98      0.94       231\n",
      "      talk.politics.misc       0.79      0.89      0.84       188\n",
      "      talk.religion.misc       0.93      0.44      0.60       158\n",
      "\n",
      "                accuracy                           0.84      4712\n",
      "               macro avg       0.86      0.84      0.82      4712\n",
      "            weighted avg       0.86      0.84      0.82      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#3.使用朴素贝叶斯进行训练\n",
    "mnb = MultinomialNB()   # 使用默认配置初始化朴素贝叶斯\n",
    "mnb.fit(X_train,y_train)    # 利用训练数据对模型参数进行估计\n",
    "mnb_predict = mnb.predict(X_test)     # 对参数进行预测\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of Naive Bayes Classifier is:', mnb.score(X_test,y_test))\n",
    "print(classification_report(y_test, mnb_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 高斯贝叶斯GaussianNB  \n",
    "### 该方法不合适"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# gnb=GaussianNB()\n",
    "# gnb.fit(X_train,y_train)\n",
    "# y_predict=gnb.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## 伯努利贝叶斯BernoulliNB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of 伯努利贝叶斯BernoulliNB is: 0.706918505942275\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.89      0.51      0.65       201\n",
      "           comp.graphics       0.81      0.62      0.70       250\n",
      " comp.os.ms-windows.misc       0.80      0.03      0.06       248\n",
      "comp.sys.ibm.pc.hardware       0.40      0.90      0.55       240\n",
      "   comp.sys.mac.hardware       0.52      0.84      0.64       242\n",
      "          comp.windows.x       0.97      0.65      0.78       263\n",
      "            misc.forsale       0.40      0.91      0.55       257\n",
      "               rec.autos       0.54      0.85      0.66       238\n",
      "         rec.motorcycles       0.97      0.85      0.91       276\n",
      "      rec.sport.baseball       0.90      0.86      0.88       251\n",
      "        rec.sport.hockey       0.97      0.92      0.94       233\n",
      "               sci.crypt       0.89      0.83      0.86       238\n",
      "         sci.electronics       0.85      0.79      0.82       249\n",
      "                 sci.med       0.95      0.73      0.83       245\n",
      "               sci.space       0.89      0.84      0.86       221\n",
      "  soc.religion.christian       0.63      0.81      0.70       232\n",
      "      talk.politics.guns       0.87      0.74      0.80       251\n",
      "   talk.politics.mideast       0.95      0.71      0.81       231\n",
      "      talk.politics.misc       0.95      0.40      0.57       188\n",
      "      talk.religion.misc       1.00      0.01      0.02       158\n",
      "\n",
      "                accuracy                           0.71      4712\n",
      "               macro avg       0.81      0.69      0.68      4712\n",
      "            weighted avg       0.80      0.71      0.69      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bnb=BernoulliNB()\n",
    "bnb.fit(X_train,y_train)\n",
    "bnb_predict=bnb.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of 伯努利贝叶斯BernoulliNB is:', bnb.score(X_test,y_test))\n",
    "print(classification_report(y_test, bnb_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3-1 逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 岭回归分类 莫名其妙卡主"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rc=RidgeClassifier()\n",
    "rc.fit(X_train,y_train)\n",
    "rc_predict=rc.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of RidgeClassifier is:', rc.score(X_test,y_test))\n",
    "print(classification_report(y_test, rc_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 3-2逻辑回归分类 ???为啥运转那么慢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lr=LogisticRegression(multi_class=\"multinomial\",solver=\"newton-cg\")\n",
    "lr.fit(X_train,y_train)\n",
    "lr_predict=rc.predict(X_test)\n",
    "#4.获取结果报告????\n",
    "print('The Accuracy of LogisticRegression is:', lr.score(X_test,y_test))\n",
    "print(classification_report(y_test, lr_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 3-3 多层感知机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of MLPClassifier is: 0.053268251273344655\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.00      0.00      0.00       201\n",
      "           comp.graphics       0.00      0.00      0.00       250\n",
      " comp.os.ms-windows.misc       0.00      0.00      0.00       248\n",
      "comp.sys.ibm.pc.hardware       0.00      0.00      0.00       240\n",
      "   comp.sys.mac.hardware       0.00      0.00      0.00       242\n",
      "          comp.windows.x       0.00      0.00      0.00       263\n",
      "            misc.forsale       0.00      0.00      0.00       257\n",
      "               rec.autos       0.00      0.00      0.00       238\n",
      "         rec.motorcycles       0.00      0.00      0.00       276\n",
      "      rec.sport.baseball       0.05      0.99      0.10       251\n",
      "        rec.sport.hockey       0.00      0.00      0.00       233\n",
      "               sci.crypt       0.00      0.00      0.00       238\n",
      "         sci.electronics       0.00      0.00      0.00       249\n",
      "                 sci.med       0.00      0.00      0.00       245\n",
      "               sci.space       0.00      0.00      0.00       221\n",
      "  soc.religion.christian       0.04      0.01      0.01       232\n",
      "      talk.politics.guns       0.00      0.00      0.00       251\n",
      "   talk.politics.mideast       0.00      0.00      0.00       231\n",
      "      talk.politics.misc       0.00      0.00      0.00       188\n",
      "      talk.religion.misc       0.00      0.00      0.00       158\n",
      "\n",
      "                accuracy                           0.05      4712\n",
      "               macro avg       0.00      0.05      0.01      4712\n",
      "            weighted avg       0.00      0.05      0.01      4712\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\anaconda\\lib\\site-packages\\sklearn\\metrics\\_classification.py:1268: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, msg_start, len(result))\n"
     ]
    }
   ],
   "source": [
    "mlp= MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(30,20), random_state=1)\n",
    "mlp.fit(X_train,y_train)\n",
    "mlp_predict=mlp.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of MLPClassifier is:', mlp.score(X_test,y_test))\n",
    "print(classification_report(y_test, mlp_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 3-4 lightBLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# gbm = LGBMClassifier(objective='multiclass',num_leaves=31,learning_rate=0.05,n_estimators=20)\n",
    "# gbm.fit(X_train, y_train,eval_set=[(X_test, y_test)],eval_metric='l1',early_stopping_rounds=5)\n",
    "# gbm_predict = gbm.predict(X_test, num_iteration=gbm.best_iteration_)\n",
    "# #4.获取结果报告\n",
    "# print('The Accuracy of LGBMClassifier is:', gbm.score(X_test,y_test))\n",
    "# print(classification_report(y_test, gbm_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 3-5 决策树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of DecisionTreeClassifier is: 0.49087436332767403\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.49      0.49      0.49       201\n",
      "           comp.graphics       0.33      0.31      0.32       250\n",
      " comp.os.ms-windows.misc       0.56      0.59      0.57       248\n",
      "comp.sys.ibm.pc.hardware       0.33      0.30      0.31       240\n",
      "   comp.sys.mac.hardware       0.48      0.46      0.47       242\n",
      "          comp.windows.x       0.43      0.43      0.43       263\n",
      "            misc.forsale       0.67      0.67      0.67       257\n",
      "               rec.autos       0.51      0.61      0.56       238\n",
      "         rec.motorcycles       0.70      0.64      0.67       276\n",
      "      rec.sport.baseball       0.45      0.50      0.47       251\n",
      "        rec.sport.hockey       0.65      0.63      0.64       233\n",
      "               sci.crypt       0.69      0.61      0.65       238\n",
      "         sci.electronics       0.25      0.26      0.25       249\n",
      "                 sci.med       0.24      0.28      0.26       245\n",
      "               sci.space       0.52      0.52      0.52       221\n",
      "  soc.religion.christian       0.62      0.68      0.65       232\n",
      "      talk.politics.guns       0.51      0.49      0.50       251\n",
      "   talk.politics.mideast       0.62      0.56      0.59       231\n",
      "      talk.politics.misc       0.39      0.38      0.39       188\n",
      "      talk.religion.misc       0.39      0.34      0.36       158\n",
      "\n",
      "                accuracy                           0.49      4712\n",
      "               macro avg       0.49      0.49      0.49      4712\n",
      "            weighted avg       0.49      0.49      0.49      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "dtc = DecisionTreeClassifier(criterion='entropy',random_state=22,splitter='random'\n",
    "#                                ,max_depth=10,min_samples_leaf=3,min_samples_split=3\n",
    "                               ) #方便剪叶子  不剪叶子效果不错\n",
    "dtc.fit(X_train, y_train)\n",
    "dtc_predict = dtc.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of DecisionTreeClassifier is:', dtc.score(X_test,y_test))\n",
    "print(classification_report(y_test, dtc_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 3-5 随机森林"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of RandomForestClassifier is: 0.8478353140916808\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.89      0.79      0.84       201\n",
      "           comp.graphics       0.64      0.77      0.70       250\n",
      " comp.os.ms-windows.misc       0.78      0.85      0.81       248\n",
      "comp.sys.ibm.pc.hardware       0.71      0.71      0.71       240\n",
      "   comp.sys.mac.hardware       0.79      0.83      0.81       242\n",
      "          comp.windows.x       0.88      0.86      0.87       263\n",
      "            misc.forsale       0.76      0.92      0.83       257\n",
      "               rec.autos       0.84      0.89      0.86       238\n",
      "         rec.motorcycles       0.95      0.92      0.93       276\n",
      "      rec.sport.baseball       0.90      0.89      0.89       251\n",
      "        rec.sport.hockey       0.90      0.97      0.94       233\n",
      "               sci.crypt       0.93      0.93      0.93       238\n",
      "         sci.electronics       0.85      0.61      0.71       249\n",
      "                 sci.med       0.89      0.87      0.88       245\n",
      "               sci.space       0.91      0.94      0.92       221\n",
      "  soc.religion.christian       0.77      0.95      0.85       232\n",
      "      talk.politics.guns       0.89      0.95      0.92       251\n",
      "   talk.politics.mideast       0.97      0.95      0.96       231\n",
      "      talk.politics.misc       0.98      0.76      0.86       188\n",
      "      talk.religion.misc       0.90      0.46      0.61       158\n",
      "\n",
      "                accuracy                           0.85      4712\n",
      "               macro avg       0.86      0.84      0.84      4712\n",
      "            weighted avg       0.85      0.85      0.85      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# #随机森林\n",
    "# clf_rfc=RandomForestClassifier(random_state=22)\n",
    "# # .fit(x_train,y_train)\n",
    "# #集成模块\n",
    "# clf_bgc=BaggingClassifier(random_state=22)\n",
    "# # .fit(x_train,y_train)\n",
    "# clf_adc=AdaBoostClassifier(learning_rate=0.1,random_state=22)\n",
    "\n",
    "rfc = RandomForestClassifier(random_state=22)\n",
    "rfc.fit(X_train, y_train)\n",
    "rfc_predict = rfc.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of RandomForestClassifier is:', rfc.score(X_test,y_test))\n",
    "print(classification_report(y_test, rfc_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3-6 AdaBoost、lightGBM(GBDT)和xgBoost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# AdaBoost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of AdaBoostClassifier is: 0.3234295415959253\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.87      0.37      0.52       201\n",
      "           comp.graphics       0.56      0.25      0.34       250\n",
      " comp.os.ms-windows.misc       0.40      0.01      0.02       248\n",
      "comp.sys.ibm.pc.hardware       0.06      0.01      0.02       240\n",
      "   comp.sys.mac.hardware       0.68      0.50      0.58       242\n",
      "          comp.windows.x       0.16      0.16      0.16       263\n",
      "            misc.forsale       0.79      0.60      0.68       257\n",
      "               rec.autos       0.62      0.54      0.58       238\n",
      "         rec.motorcycles       1.00      0.19      0.32       276\n",
      "      rec.sport.baseball       0.41      0.39      0.40       251\n",
      "        rec.sport.hockey       1.00      0.22      0.36       233\n",
      "               sci.crypt       0.99      0.42      0.59       238\n",
      "         sci.electronics       0.04      0.06      0.04       249\n",
      "                 sci.med       0.11      0.93      0.19       245\n",
      "               sci.space       0.66      0.44      0.53       221\n",
      "  soc.religion.christian       0.52      0.59      0.55       232\n",
      "      talk.politics.guns       0.73      0.11      0.19       251\n",
      "   talk.politics.mideast       1.00      0.37      0.54       231\n",
      "      talk.politics.misc       0.33      0.21      0.25       188\n",
      "      talk.religion.misc       0.35      0.05      0.09       158\n",
      "\n",
      "                accuracy                           0.32      4712\n",
      "               macro avg       0.56      0.32      0.35      4712\n",
      "            weighted avg       0.57      0.32      0.35      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "adc = AdaBoostClassifier(learning_rate=0.1,random_state=22)\n",
    "adc.fit(X_train, y_train)\n",
    "adc_predict = adc.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of AdaBoostClassifier is:', adc.score(X_test,y_test))\n",
    "print(classification_report(y_test, adc_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# xgboost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'lightgbm' has no attribute 'XGBClassifier'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-c9d36960a7f5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mxgbc\u001b[0m \u001b[1;33m=\u001b[0m\u001b[0mlgb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mXGBClassifier\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlearning_rate\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;36m0.3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmax_depth\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mseed\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1000\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mxgbc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mxgbc_predict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mxgbc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m#4.获取结果报告\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'The Accuracy of XGBClassifier is:'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mxgbc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_test\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'lightgbm' has no attribute 'XGBClassifier'"
     ]
    }
   ],
   "source": [
    "xgbc =xgboost.XGBClassifier(learning_rate= 0.3,max_depth=6, seed=1000)\n",
    "xgbc.fit(X_train, y_train)\n",
    "xgbc_predict = xgbc.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of XGBClassifier is:', xgbc.score(X_test,y_test))\n",
    "print(classification_report(y_test, xgbc_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# lightGBM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "exception: access violation reading 0x00000000000002A8",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\engine.py\u001b[0m in \u001b[0;36mtrain\u001b[1;34m(params, train_set, num_boost_round, valid_sets, valid_names, fobj, feval, init_model, feature_name, categorical_feature, early_stopping_rounds, evals_result, verbose_eval, learning_rates, keep_training_booster, callbacks)\u001b[0m\n\u001b[0;32m    227\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 228\u001b[1;33m         \u001b[0mbooster\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mBooster\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrain_set\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtrain_set\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    229\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mis_valid_contain_train\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, params, train_set, model_file, model_str, silent)\u001b[0m\n\u001b[0;32m   1713\u001b[0m             _safe_call(_LIB.LGBM_BoosterCreate(\n\u001b[1;32m-> 1714\u001b[1;33m                 \u001b[0mtrain_set\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconstruct\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1715\u001b[0m                 \u001b[0mc_str\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams_str\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36mconstruct\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1084\u001b[0m                                 \u001b[0msilent\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msilent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeature_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfeature_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1085\u001b[1;33m                                 categorical_feature=self.categorical_feature, params=self.params)\n\u001b[0m\u001b[0;32m   1086\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfree_raw_data\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36m_lazy_init\u001b[1;34m(self, data, label, reference, weight, group, init_score, predictor, silent, feature_name, categorical_feature, params)\u001b[0m\n\u001b[0;32m    882\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscipy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msparse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcsr_matrix\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 883\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__init_from_csr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams_str\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mref_dataset\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    884\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscipy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msparse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcsc_matrix\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36m__init_from_csr\u001b[1;34m(self, csr, params_str, ref_dataset)\u001b[0m\n\u001b[0;32m    992\u001b[0m         \u001b[0mptr_indptr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtype_ptr_indptr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m__\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mc_int_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcsr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 993\u001b[1;33m         \u001b[0mptr_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtype_ptr_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mc_float_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcsr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    994\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36mc_float_array\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m    273\u001b[0m             raise TypeError(\"Expected np.float32 or np.float64, met type({})\"\n\u001b[1;32m--> 274\u001b[1;33m                             .format(data.dtype))\n\u001b[0m\u001b[0;32m    275\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: Expected np.float32 or np.float64, met type(int64)",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-22d03b3d3472>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mlgbm\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mLGBMClassifier\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mlgbm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mlgbm_predict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlgbm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m#4.获取结果报告\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'The Accuracy of  LGBMClassifier is:'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlgbm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_test\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\sklearn.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, init_score, eval_set, eval_names, eval_sample_weight, eval_class_weight, eval_init_score, eval_metric, early_stopping_rounds, verbose, feature_name, categorical_feature, callbacks)\u001b[0m\n\u001b[0;32m    803\u001b[0m                                         \u001b[0mverbose\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mverbose\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeature_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfeature_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    804\u001b[0m                                         \u001b[0mcategorical_feature\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcategorical_feature\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 805\u001b[1;33m                                         callbacks=callbacks)\n\u001b[0m\u001b[0;32m    806\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    807\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\sklearn.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, init_score, group, eval_set, eval_names, eval_sample_weight, eval_class_weight, eval_init_score, eval_group, eval_metric, early_stopping_rounds, verbose, feature_name, categorical_feature, callbacks)\u001b[0m\n\u001b[0;32m    598\u001b[0m                               \u001b[0mverbose_eval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mverbose\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeature_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfeature_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    599\u001b[0m                               \u001b[0mcategorical_feature\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcategorical_feature\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 600\u001b[1;33m                               callbacks=callbacks)\n\u001b[0m\u001b[0;32m    601\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    602\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mevals_result\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\engine.py\u001b[0m in \u001b[0;36mtrain\u001b[1;34m(params, train_set, num_boost_round, valid_sets, valid_names, fobj, feval, init_model, feature_name, categorical_feature, early_stopping_rounds, evals_result, verbose_eval, learning_rates, keep_training_booster, callbacks)\u001b[0m\n\u001b[0;32m    232\u001b[0m             \u001b[0mbooster\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_valid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalid_set\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname_valid_set\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    233\u001b[0m     \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 234\u001b[1;33m         \u001b[0mtrain_set\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_reverse_update_params\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    235\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mvalid_set\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mreduced_valid_sets\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    236\u001b[0m             \u001b[0mvalid_set\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_reverse_update_params\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\anaconda\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36m_reverse_update_params\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1179\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparams_back_up\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1180\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparams\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1181\u001b[1;33m             \u001b[0m_safe_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_LIB\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLGBM_DatasetUpdateParam\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mc_str\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparam_dict_to_str\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1182\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1183\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mOSError\u001b[0m: exception: access violation reading 0x00000000000002A8"
     ]
    }
   ],
   "source": [
    "lgbm = LGBMClassifier()\n",
    "lgbm.fit(X_train, y_train)\n",
    "lgbm_predict = lgbm.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of  LGBMClassifier is:', lgbm.score(X_test,y_test))\n",
    "print(classification_report(y_test, lgbm_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3-7 KNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of  KNeighborsClassifier is: 0.5072156196943973\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.32      0.63      0.43       201\n",
      "           comp.graphics       0.27      0.47      0.35       250\n",
      " comp.os.ms-windows.misc       0.47      0.54      0.50       248\n",
      "comp.sys.ibm.pc.hardware       0.57      0.41      0.48       240\n",
      "   comp.sys.mac.hardware       0.43      0.43      0.43       242\n",
      "          comp.windows.x       0.71      0.37      0.49       263\n",
      "            misc.forsale       0.56      0.60      0.58       257\n",
      "               rec.autos       0.44      0.42      0.43       238\n",
      "         rec.motorcycles       0.79      0.55      0.65       276\n",
      "      rec.sport.baseball       0.65      0.45      0.53       251\n",
      "        rec.sport.hockey       0.63      0.62      0.62       233\n",
      "               sci.crypt       0.61      0.70      0.65       238\n",
      "         sci.electronics       0.72      0.33      0.45       249\n",
      "                 sci.med       0.60      0.48      0.53       245\n",
      "               sci.space       0.88      0.48      0.63       221\n",
      "  soc.religion.christian       0.32      0.63      0.42       232\n",
      "      talk.politics.guns       0.70      0.53      0.61       251\n",
      "   talk.politics.mideast       0.41      0.70      0.51       231\n",
      "      talk.politics.misc       0.69      0.44      0.54       188\n",
      "      talk.religion.misc       0.62      0.34      0.43       158\n",
      "\n",
      "                accuracy                           0.51      4712\n",
      "               macro avg       0.57      0.51      0.51      4712\n",
      "            weighted avg       0.57      0.51      0.52      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "knn =KNeighborsClassifier(n_neighbors=5)\n",
    "knn.fit(X_train, y_train)\n",
    "knn_predict = knn.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of  KNeighborsClassifier is:', knn.score(X_test,y_test))\n",
    "print(classification_report(y_test, knn_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3-8 SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "svc =SVC(kernel='rbf')\n",
    "# clf=SVC(kernel='linear')\n",
    "# clf1=SVC(kernel='poly')\n",
    "# clf2=SVC(kernel='rbf')  #还是这个核比较强大\n",
    "# clf3=SVC(kernel='sigmoid')\n",
    "svc.fit(X_train, y_train)\n",
    "svc_predict = svc.predict(X_test)\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of  SVC is:', svc.score(X_test,y_test))\n",
    "print(classification_report(y_test, svc_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 还有没有调用的法术"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#深度学习\n",
    "\n",
    "# LSTM \n",
    "# 前馈神经网络"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.获取结果报告"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of Naive Bayes Classifier is: 0.8397707979626485\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.86      0.86      0.86       201\n",
      "           comp.graphics       0.59      0.86      0.70       250\n",
      " comp.os.ms-windows.misc       0.89      0.10      0.17       248\n",
      "comp.sys.ibm.pc.hardware       0.60      0.88      0.72       240\n",
      "   comp.sys.mac.hardware       0.93      0.78      0.85       242\n",
      "          comp.windows.x       0.82      0.84      0.83       263\n",
      "            misc.forsale       0.91      0.70      0.79       257\n",
      "               rec.autos       0.89      0.89      0.89       238\n",
      "         rec.motorcycles       0.98      0.92      0.95       276\n",
      "      rec.sport.baseball       0.98      0.91      0.95       251\n",
      "        rec.sport.hockey       0.93      0.99      0.96       233\n",
      "               sci.crypt       0.86      0.98      0.91       238\n",
      "         sci.electronics       0.85      0.88      0.86       249\n",
      "                 sci.med       0.92      0.94      0.93       245\n",
      "               sci.space       0.89      0.96      0.92       221\n",
      "  soc.religion.christian       0.78      0.96      0.86       232\n",
      "      talk.politics.guns       0.88      0.96      0.92       251\n",
      "   talk.politics.mideast       0.90      0.98      0.94       231\n",
      "      talk.politics.misc       0.79      0.89      0.84       188\n",
      "      talk.religion.misc       0.93      0.44      0.60       158\n",
      "\n",
      "                accuracy                           0.84      4712\n",
      "               macro avg       0.86      0.84      0.82      4712\n",
      "            weighted avg       0.86      0.84      0.82      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#4.获取结果报告\n",
    "print('The Accuracy of Naive Bayes Classifier is:', mnb.score(X_test,y_test))\n",
    "print(classification_report(y_test, y_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of 伯努利贝叶斯BernoulliNB is: 0.706918505942275\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.89      0.51      0.65       201\n",
      "           comp.graphics       0.81      0.62      0.70       250\n",
      " comp.os.ms-windows.misc       0.80      0.03      0.06       248\n",
      "comp.sys.ibm.pc.hardware       0.40      0.90      0.55       240\n",
      "   comp.sys.mac.hardware       0.52      0.84      0.64       242\n",
      "          comp.windows.x       0.97      0.65      0.78       263\n",
      "            misc.forsale       0.40      0.91      0.55       257\n",
      "               rec.autos       0.54      0.85      0.66       238\n",
      "         rec.motorcycles       0.97      0.85      0.91       276\n",
      "      rec.sport.baseball       0.90      0.86      0.88       251\n",
      "        rec.sport.hockey       0.97      0.92      0.94       233\n",
      "               sci.crypt       0.89      0.83      0.86       238\n",
      "         sci.electronics       0.85      0.79      0.82       249\n",
      "                 sci.med       0.95      0.73      0.83       245\n",
      "               sci.space       0.89      0.84      0.86       221\n",
      "  soc.religion.christian       0.63      0.81      0.70       232\n",
      "      talk.politics.guns       0.87      0.74      0.80       251\n",
      "   talk.politics.mideast       0.95      0.71      0.81       231\n",
      "      talk.politics.misc       0.95      0.40      0.57       188\n",
      "      talk.religion.misc       1.00      0.01      0.02       158\n",
      "\n",
      "                accuracy                           0.71      4712\n",
      "               macro avg       0.81      0.69      0.68      4712\n",
      "            weighted avg       0.80      0.71      0.69      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#4.获取结果报告\n",
    "print('The Accuracy of 伯努利贝叶斯BernoulliNB is:', bnb.score(X_test,y_test))\n",
    "print(classification_report(y_test, y_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18846\n",
      "The Accuracy of Naive Bayes Classifier is: 0.8397707979626485\n",
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.86      0.86      0.86       201\n",
      "           comp.graphics       0.59      0.86      0.70       250\n",
      " comp.os.ms-windows.misc       0.89      0.10      0.17       248\n",
      "comp.sys.ibm.pc.hardware       0.60      0.88      0.72       240\n",
      "   comp.sys.mac.hardware       0.93      0.78      0.85       242\n",
      "          comp.windows.x       0.82      0.84      0.83       263\n",
      "            misc.forsale       0.91      0.70      0.79       257\n",
      "               rec.autos       0.89      0.89      0.89       238\n",
      "         rec.motorcycles       0.98      0.92      0.95       276\n",
      "      rec.sport.baseball       0.98      0.91      0.95       251\n",
      "        rec.sport.hockey       0.93      0.99      0.96       233\n",
      "               sci.crypt       0.86      0.98      0.91       238\n",
      "         sci.electronics       0.85      0.88      0.86       249\n",
      "                 sci.med       0.92      0.94      0.93       245\n",
      "               sci.space       0.89      0.96      0.92       221\n",
      "  soc.religion.christian       0.78      0.96      0.86       232\n",
      "      talk.politics.guns       0.88      0.96      0.92       251\n",
      "   talk.politics.mideast       0.90      0.98      0.94       231\n",
      "      talk.politics.misc       0.79      0.89      0.84       188\n",
      "      talk.religion.misc       0.93      0.44      0.60       158\n",
      "\n",
      "                accuracy                           0.84      4712\n",
      "               macro avg       0.86      0.84      0.82      4712\n",
      "            weighted avg       0.86      0.84      0.82      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#1.数据获取\n",
    "news = fetch_20newsgroups(subset='all')\n",
    "print(len(news.data))  # 输出数据的条数：18846\n",
    "\n",
    "#2.数据预处理：训练集和测试集分割，文本特征向量化\n",
    "X_train,X_test,y_train,y_test = train_test_split(news.data,news.target,test_size=0.25,random_state=33) # 随机采样25%的数据样本作为测试集\n",
    "#print X_train[0]  #查看训练样本\n",
    "#print y_train[0:100]  #查看标签\n",
    "\n",
    "#文本特征向量化\n",
    "vec = CountVectorizer()\n",
    "X_train = vec.fit_transform(X_train)\n",
    "X_test = vec.transform(X_test)\n",
    "\n",
    "#3.使用朴素贝叶斯进行训练\n",
    "mnb = MultinomialNB()   # 使用默认配置初始化朴素贝叶斯\n",
    "mnb.fit(X_train,y_train)    # 利用训练数据对模型参数进行估计\n",
    "y_predict = mnb.predict(X_test)     # 对参数进行预测\n",
    "\n",
    "#4.获取结果报告\n",
    "print('The Accuracy of Naive Bayes Classifier is:', mnb.score(X_test,y_test))\n",
    "print(classification_report(y_test, y_predict, target_names = news.target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
